package com.activity.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.activity.bean.ClassBean;
import com.activity.bean.ClassSpan;
import com.api.param.ClassParam;
import com.api.param.DeleteClassParam;
import com.api.param.StoreParam;
import com.api.viewModel.StoreVM;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.widget.Dialog;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.example.lzbase.BaseViewModelActivity;
import com.google.gson.Gson;

import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import event.MessageEvent;
import retrofit2.http.GET;

public class NewClassActivity extends BaseViewModelActivity<StoreVM> {


    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_begin_time)
    TextView tvBeginTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.btn_sure)
    Button btnSure;
    private ClassSpan classSpan;
    private ClassParam param;

    @Override
    protected int titleText() {
        return R.string.xinzen_banci;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_new_class;
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {
        super.created(savedInstanceState);
        viewModel.addClass.observe(this,res->{

            finish();
            EventBus.getDefault().post(new MessageEvent(MessageEvent.MESSAGE_EVENT_ADD_CLASS));

        });
        viewModel.updateClass.observe(this,res->{

            finish();
            MessageEvent messageEvent=  new MessageEvent(MessageEvent.MESSAGE_EVENT_UPDATE_CLASS);
            Gson gson = new Gson();
            messageEvent.spanParam  =   gson.toJson(param);
            EventBus.getDefault().post(messageEvent);

        });
        viewModel.deleteClass.observe(this,res->{

            finish();
            MessageEvent messageEvent=  new MessageEvent(MessageEvent.MESSAGE_EVENT_DELETE_CLASS);
            messageEvent.spamId = classSpan.getAttendacetimespanid();
            EventBus.getDefault().post(messageEvent);

        });

        if(getIntent().getExtras()!=null&&getIntent().getExtras().getString("param")!=null){
            Gson gson =new Gson();

             classSpan = gson.fromJson(getIntent().getExtras().getString("param"),ClassSpan.class);

             setTitleRight("删除");

             etName.setText(classSpan.getAttendacetimespanname());
             tvBeginTime.setText(classSpan.getAttendacetimespan_begintime());
             tvEndTime.setText(classSpan.getAttendacetimespan_endtime());
        }
    }

    @Override
    protected void titleRightClick() {

        DeleteClassParam param = new DeleteClassParam();
        param.setAttendacetimespanid(classSpan.getAttendacetimespanid());
        addSubscription(viewModel.deleteClass(param));
    }

    @OnClick({R.id.l_begin, R.id.l_end})
    public void onViewClicked(View view) {
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        Calendar selectDate = Calendar.getInstance();
        switch (view.getId()) {
            case R.id.l_begin:


            {

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");


                try {
                    min.setTime(simpleDateFormat.parse("00:00"));

                } catch (ParseException e) {

                }

                if (StringUtils.isEmpty(tvEndTime.getText())) {

                    try {
                        max.setTime(simpleDateFormat.parse("23:59"));

                    } catch (ParseException e) {

                    }
                }else {
                    try {
                        max.setTime(simpleDateFormat.parse(tvEndTime.getText().toString()));

                    } catch (ParseException e) {

                    }



                }

                TimePickerView builder = Dialog.popMinuteTime(this,min,max, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        String dateString = simpleDateFormat.format(date);
                        tvBeginTime.setText(dateString);


                    }
                });

                // 如果不设置的话，默认是系统时间*/


                if (StringUtils.isEmpty(tvBeginTime.getText())) {
                    selectDate.set(Calendar.HOUR, 0);
                    selectDate.set(Calendar.SECOND, 0);
                } else {

                    try {
                        selectDate.setTime(simpleDateFormat.parse( tvBeginTime.getText().toString()));

                    } catch (ParseException e) {

                    }
                }
                builder.setDate(selectDate);
                builder.show();
            }


            break;
            case R.id.l_end: {

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

                try {
                    max.setTime(simpleDateFormat.parse("23:59"));

                } catch (ParseException e) {

                }



                if (StringUtils.isEmpty(tvBeginTime.getText())) {

                    try {
                        min.setTime(simpleDateFormat.parse("00:00"));

                    } catch (ParseException e) {

                    }
                }else {
                    try {
                        min.setTime(simpleDateFormat.parse(tvBeginTime.getText().toString()));

                    } catch (ParseException e) {

                    }


                }

                TimePickerView builder = Dialog.popMinuteTime(this,min,max, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                        String dateString = simpleDateFormat.format(date);
                        tvEndTime.setText(dateString);


                    }
                });

                // 如果不设置的话，默认是系统时间*/


                if (StringUtils.isEmpty(tvBeginTime.getText())) {
                    selectDate.set(Calendar.HOUR, 0);
                    selectDate.set(Calendar.SECOND, 0);
                } else {

                    try {
                        selectDate.setTime(simpleDateFormat.parse(tvEndTime.getText().toString()));

                    } catch (ParseException e) {

                    }
                }
                builder.setDate(selectDate);
                builder.show();
            }


            break;
        }
    }



    @OnClick(R.id.btn_sure)
    public void onViewClicked() {

        if(StringUtils.isEmpty(etName.getText())){

            ToastUtil.toast(this,"请输入班次名称");
        }else {

            ClassParam storeParam = new ClassParam();
            storeParam.setTimespanname(etName.getText().toString());
            storeParam.setBegintime(tvBeginTime.getText().toString());
            storeParam.setEndtime(tvEndTime.getText().toString());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
            Calendar bdate = Calendar.getInstance();
            Calendar edate = Calendar.getInstance();
            try {
                bdate.setTime(simpleDateFormat.parse(tvBeginTime.getText().toString()));
                edate.setTime(simpleDateFormat.parse(tvEndTime.getText().toString()));
               long sec= (edate.getTimeInMillis() -bdate.getTimeInMillis())/1000;
               long hour =  sec/3600;
               long min =  sec%3600/60;
               storeParam.setTimelength(hour+":"+min);

            } catch (ParseException e) {

            }

            if(classSpan!=null){

                storeParam.setTimespanid(classSpan.getAttendacetimespanid());

                param = storeParam;
                addSubscription(viewModel.updateClass(storeParam));
            }else {
                addSubscription(viewModel.addClass(storeParam));
            }



        }
    }
}
